Class ListEx
	Private m_items' 存储列表元素的数组
	Private m_count' 当前元素数量
	Private m_capacity' 当前容量（预分配的数组大小）
	Private m_growFactor' 容量增长因子（默认1.5倍）

	' 类初始化
	Private Sub Class_Initialize()
		m_count = 0
		m_capacity = 4' 初始容量
		m_growFactor = 1.5' 默认增长因子
		ReDim m_items(m_capacity - 1) ' 初始化数组
	End Sub

	' 类销毁
	Private Sub Class_Terminate()
		'Erase m_items' 释放数组内存
		m_count = 0
		m_capacity = 0
	End Sub

	' ================ 属性 ================

	' 获取元素数量
	Public Property Get Count()
		Count = m_count
	End Property

	' 获取当前容量
	Public Property Get Capacity()
		Capacity = m_capacity
	End Property

	' 获取/设置容量增长因子
	Public Property Get GrowFactor()
		GrowFactor = m_growFactor
	End Property

	Public Property Let GrowFactor(value)
		If value > 1 Then m_growFactor = value
	End Property

	' 索引访问元素（支持类似数组的访问方式：list(index)）
	Public Property Get Item(index)
		If index < 0 Or index >= m_count Then
		Err.Raise 9, "ListEx.Item", "索引超出范围"
		End If
		Item = m_items(index)
	End Property

	Public Property Let Item(index, value)
		If index < 0 Or index >= m_count Then
		Err.Raise 9, "ListEx.Item", "索引超出范围"
		End If
		m_items(index) = value
	End Property
	
	' 实现类似数组的访问方式（例如 list(0) = "value"）
	Public Property Get ItemAccessor(index)
		ItemAccessor = Item(index)
	End Property

	Public Property Let ItemAccessor(index, value)
		Item(index) = value
	End Property
	' ================ 核心操作方法 ================

	' 添加元素到列表末尾
	Public Sub Add(item)
		' 检查容量，必要时扩容
		If m_count >= m_capacity Then ExpandCapacity
		m_items(m_count) = item
		m_count = m_count + 1
	End Sub

	' 在指定位置插入元素
	Public Sub Insert(index, item)
		If index < 0 Or index > m_count Then
			Err.Raise 9, "ListEx.Insert", "索引超出范围"
		End If

		' 检查容量，必要时扩容
		If m_count >= m_capacity Then ExpandCapacity

		' 移动元素腾出位置
		Dim i
		For i = m_count - 1 To index Step -1
		m_items(i + 1) = m_items(i)
		Next

		' 插入新元素
		m_items(index) = item
		m_count = m_count + 1
	End Sub

	' 删除指定索引的元素
	Public Sub RemoveAt(index)
		If index < 0 Or index >= m_count Then
			Err.Raise 9, "ListEx.RemoveAt", "索引超出范围"
		End If

		' 移动元素填补空缺
		Dim i
		For i = index To m_count - 2
			m_items(i) = m_items(i + 1)
		Next

		' 清空最后一个位置（可选）
		m_items(m_count - 1) = Empty

		m_count = m_count - 1

		' 检查是否需要缩减容量（当元素数量小于容量的1/4时）
		If m_count < m_capacity / 4 And m_capacity > 4 Then
			ShrinkCapacity
		End If
	End Sub

	' 删除指定值的第一个出现的元素
	Public Function Remove(value)
		Dim index
		index = IndexOf(value)
		If index >= 0 Then
			RemoveAt index
			Remove = True
		Else
			Remove = False
		End If
	End Function

	' 清空列表
	Public Sub Clear()
		Erase m_items
		m_count = 0
		m_capacity = 4
		ReDim m_items(m_capacity - 1)
	End Sub

	' 检查列表是否包含指定元素
	Public Function Contains(value)
		Contains = (IndexOf(value) >= 0)
	End Function

	' 查找元素并返回第一个匹配的索引（未找到返回-1）
	Public Function IndexOf(value)
		Dim i
		For i = 0 To m_count - 1
			'msgbox m_items(i) & vbcrlf & value & vbcrlf & CompareValues(m_items(i), value)
			If CompareValues(m_items(i), value)=0 Then
				IndexOf = i
				Exit Function
			End If
		Next
		IndexOf = -1
	End Function

	' 查找元素并返回最后一个匹配的索引（未找到返回-1）
	Public Function LastIndexOf(value)
		Dim i
		For i = m_count - 1 To 0 Step -1
			If CompareValues(m_items(i), value)=0 Then
				LastIndexOf = i
				Exit Function
			End If
		Next
		LastIndexOf = -1
	End Function

	' 获取列表的子列表（从startIndex开始，截取length个元素）
	Public Function SubList(startIndex, length)
		Dim osubList, i
		Set osubList = New ListEx

		If startIndex < 0 Or startIndex >= m_count Then
			Err.Raise 9, "ListEx.SubList", "起始索引超出范围"
		End If

		' 调整长度，确保不超出列表范围
		If startIndex + length > m_count Then
			length = m_count - startIndex
		End If

		' 添加子列表元素
		For i = 0 To length - 1
			osubList.Add m_items(startIndex + i)
		Next

		Set SubList = osubList
	End Function

	' 将列表转换为数组
	Public Function ToArray()
		Dim arr()
		ReDim arr(m_count - 1)

		Dim i
		For i = 0 To m_count - 1
			arr(i) = m_items(i)
		Next

		ToArray = arr
	End Function

	' 反转列表元素
	Public Sub Reverse()
		Dim i, temp
		For i = 0 To (m_count - 1) / 2
			temp = m_items(i)
			m_items(i) = m_items(m_count - 1 - i)
			m_items(m_count - 1 - i) = temp
		Next
	End Sub

	' 对列表进行排序（默认升序）
	Public Sub Sort(ascending)
		Dim i, j, temp

		' 简单冒泡排序实现（可根据需要替换为更高效的排序算法）
		For i = 0 To m_count - 2
			For j = i + 1 To m_count - 1
				If CompareValues(m_items(i), m_items(j)) > 0 Xor Not ascending Then
					' 交换元素
					temp = m_items(i)
					m_items(i) = m_items(j)
					m_items(j) = temp
				End If
			Next
		Next
	End Sub

	' ================ 辅助方法 ================

	' 扩展容量（按增长因子扩容）
	Private Sub ExpandCapacity()
		Dim newCapacity, i
		newCapacity = m_capacity * m_growFactor

		' 确保容量至少增加1（防止增长因子过小导致无法扩容）
		If newCapacity <= m_capacity Then newCapacity = m_capacity + 1

		Dim newItems()
		ReDim newItems(newCapacity - 1)

		' 复制现有元素
		For i = 0 To m_count - 1
			newItems(i) = m_items(i)
		Next

		' 替换为新数组
		'Erase m_items
		m_items = newItems
		m_capacity = newCapacity
	End Sub

	' 缩减容量（缩减为当前元素数的2倍，至少4）
	Private Sub ShrinkCapacity()
		Dim newCapacity, i
		newCapacity = m_count * 2
		If newCapacity < 4 Then newCapacity = 4

		Dim newItems()
		ReDim newItems(newCapacity - 1)

		' 复制现有元素
		For i = 0 To m_count - 1
		newItems(i) = m_items(i)
		Next

		' 替换为新数组
		'Erase m_items
		m_items = newItems
		m_capacity = newCapacity
	End Sub

	' 比较两个值（支持不同类型比较）
	Private Function CompareValues(a, b)
		' 处理Null值
		If IsNull(a) And IsNull(b) Then
			CompareValues = 0
			Exit Function
		ElseIf IsNull(a) Then
			CompareValues = -1
			Exit Function
		ElseIf IsNull(b) Then
			CompareValues = 1
			Exit Function
		End If

		' 处理Empty值
		If IsEmpty(a) And IsEmpty(b) Then
			CompareValues = 0
			Exit Function
		ElseIf IsEmpty(a) Then
			CompareValues = -1
			Exit Function
		ElseIf IsEmpty(b) Then
			CompareValues = 1
			Exit Function
		End If

		' 处理对象引用
		If IsObject(a) And IsObject(b) Then
			Set CompareValues = (a Is b)
			Exit Function
		ElseIf IsObject(a) Then
			CompareValues = 1
			Exit Function
		ElseIf IsObject(b) Then
			CompareValues = -1
			Exit Function
		End If

		' 处理数值比较
		If IsNumeric(a) And IsNumeric(b) Then
			CompareValues = CDbl(a) - CDbl(b)
			Exit Function
		End If

		' 处理日期比较
		If IsDate(a) And IsDate(b) Then
			CompareValues = DateDiff("s", a, b)
			Exit Function
		End If

		' 处理布尔值比较
		If VarType(a) = vbBoolean And VarType(b) = vbBoolean Then
		If a = b Then
			CompareValues = 0
		ElseIf a Then
			CompareValues = 1
		Else
			CompareValues = -1
		End If
		
		'Exit Function
		End If

		' 默认字符串比较（不区分大小写）
		CompareValues = StrComp(CStr(a), CStr(b), vbTextCompare)
	End Function


End Class